<template>
	<view class="main">
		<ul class="list">
			<!-- <li>
				<image src="@/static/img/wx.png" mode=""></image>
				<p>微信支付</p>
				<u-radio-group class="sele" v-model="radiovalue1" placement="column" @change="groupChange">
					<u-radio name="1"></u-radio>
				</u-radio-group>
			</li> -->
			<li>
				<image src="@/static/img/zfb.png" mode=""></image>
				<p>支付宝支付</p>
				<u-radio-group class="sele" v-model="radiovalue1" placement="column" @change="groupChange">
					<u-radio name="2"></u-radio>
				</u-radio-group>
			</li>
		</ul>
		<view class="bottom">
			<view class="in">
				<view class="price">￥{{ tagInfo.total + tagInfo.transfee }}</view>
				<view class="btn" v-if="!ispay">订单支付</view>
				<view class="btn" v-else @tap="pay">订单支付</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getOrder,
		payNow,
		checkOrder
	} from '@/api/user.js';
	export default {
		data() {
			return {
				ispay: true,
				radiovalue1: '2',
				paytype: 'alipay',
				tagInfo: {},
				orderInfo: {},
				tag: '',
				gopay: false
			};
		},
		onLoad(e) {
			this.tag = e.tag;
			this.getInfo();
		},
		onShow() {
			if (this.gopay) {
				checkOrder({
					order_num: this.tag
				}).then(res => {
					if (res.data.status == 0) {
						this.ispay = true
						uni.showToast({
							title: '支付失败',
							icon: 'none'
						})
						this.gopay = false
					} else {
						this.ispay = false
						uni.showToast({
							title: '支付成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}, 1000)
					}
				})
			}
		},
		methods: {
			async pay() {
				let res = await payNow({
					order_num: this.tag,
					paytype: this.paytype
				})
				this.extraData = res.data
				this.gopay = true
				if (this.radiovalue1 == 1) {
					plus.share.getServices(
						(res1) => {
							this.sweixin = res1.find((i) => i.id === 'weixin');
							if (this.sweixin) {
								// 分享跳转到微信小程序
								this.sweixin.launchMiniProgram({
										id: 'gh_e64a1a89a0ad', //关联微信小程序的原始ID（"g_"开头的字符串）
										type: 0,
										path: `pages/orderDetail/orderDetail?cusid=${this.extraData.cusid}&appid=${this.extraData.appid}&trxamt=${this.extraData.trxamt}&reqsn=${this.extraData.reqsn}&notify_url=${this.extraData.notify_url}&body=${this.extraData.body}&remark=${this.extraData.remark}&randomstr=${this.extraData.randomstr}&paytype=${this.extraData.paytype}&sign=${this.extraData.sign}&version=${this.extraData.version}&signtype=${this.extraData.signtype}`
									},
									(res) => {},
									(err) => {}
								);
							}
						},
					);
				} 
				if (this.radiovalue1 == 2) {
					var payinfo = {
						cusid: this.extraData.cusid,
						appid: this.extraData.appid,
						trxamt: this.extraData.trxamt,
						notify_url: this.extraData.notify_url,
						body: this.extraData.body,
						remark: this.extraData.remark,
						randomstr: this.extraData.randomstr,
						paytype: this.extraData.paytype,
						sign: this.extraData.sign,
						version: this.extraData.version,
						signtype: this.extraData.signtype,
						reqsn: this.extraData.reqsn
					}
					var query = encodeURIComponent("payinfo=" + encodeURIComponent(JSON.stringify(payinfo)));
					
					let action = ''
					if(uni.getSystemInfoSync().platform == "ios"){
						var url =
							"alipay://platformapi/startapp?appId=2021001104615521&page=pages/orderDetail/orderDetail&thirdPartSchema=" +
							"allinpaysdk://" +
							`&query=${query}`
						plus.runtime.openURL(url);
					}else{
						var url =
							"alipays://platformapi/startapp?appId=2021001104615521&page=pages/orderDetail/orderDetail&thirdPartSchema=" +
							"allinpaysdk://" +
							`&query=${query}`
						if (plus.runtime.isApplicationExist({
								pname: 'com.eg.android.AlipayGphone',
								action: 'alipay://'
							})) {
							plus.runtime.openURL(url);
						} else {
							uni.showToast({
								title:'请先安装支付宝APP',
								icon:'none'
							})
						}
					}
				}
			},
			groupChange(e) {
				// this.paytype = e
				// console.log(this.paytype)
				if (e == 1) {
					this.paytype = 'wechat'
				} else {
					this.paytype = 'alipay'
				}
			},
			async getInfo() {
				let res = await getOrder({
					tag: this.tag
				});
				this.tagInfo = res.data;
			}
		}
	};
</script>

<style lang="scss" scoped>
	.main {
		min-height: calc(100vh - 44px);
		background-color: #f8f8f8;
		padding: 15px;

		.list {
			li {
				display: flex;
				align-items: center;
				padding: 18px 0 18px 20px;
				background-color: #fff;
				border-radius: 6px;
				margin-bottom: 12px;
				font-size: 14px;
				color: #3d3d3d;
				position: relative;

				image {
					width: 40px;
					height: 40px;
					margin-right: 15px;
				}

				.sele {
					position: absolute;
					right: 15px;
				}
			}
		}

		.bottom {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100vw;
			background-color: #fff;

			.in {
				padding: 10px 15px;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.price {
					color: #e1222a;
					font-size: 20px;
					font-weight: 700;
				}

				.btn {
					font-size: 12px;
					padding: 10px 24px;
					background-color: #0a81f8;
					border-radius: 100px;
					color: #fff;
				}
			}
		}
	}
</style>